#include "pch.h"
#include "delreporttask.h"

#include "util.h"
#include "crpc.h"
#include "util/dbutil.h"
#include "utildb/sqlbuilder.h"

DelReportTask::DelReportTask()
:CMysqlTask("DelReportTask")
,m_rpcId(0)
,m_roleId(0)
{
}

DelReportTask::~DelReportTask()
{
}

void DelReportTask::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
{
	SetMysqlError(MYSQL_ERR_FAILED);
	std::string sql = "delete from rolereport where _id=" + ToString(m_roleId);
	if(!poMysqlConn->Query(sql.c_str(), sql.size(), NULL))
	{
		GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
		return;
	}

	SetMysqlError(MYSQL_ERR_SUCESS);
}

void DelReportTask::OnReply()
{
	KKSG::ErrorCode nErrCode = KKSG::ERR_SUCCESS;
	if(m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		nErrCode = KKSG::ERR_FAILED;
		LogError("Delete rolereport failed[%llu], errMsg: %s", m_roleId, GetErrStream().str().c_str());
	}

	CUserData oUser((UINT32)nErrCode, NULL);
	CRpc::ReplyDelayRpc(m_rpcId, oUser);
}

DelReportTask2::DelReportTask2()
	:DBTaskBase("DelReportTask")
	, m_rpcId(0)
	, m_roleId(0)
{
}

DelReportTask2::~DelReportTask2()
{
}

// void DelReportTask2::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
// {
// 	SetMysqlError(MYSQL_ERR_FAILED);
// 	std::string sql = "delete from rolereport where _id=" + ToString(m_roleId);
// 	if (!poMysqlConn->Query(sql.c_str(), sql.size(), NULL))
// 	{
// 		GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
// 		return;
// 	}
// 
// 	SetMysqlError(MYSQL_ERR_SUCESS);
// }

// void DelReportTask2::OnReply()
// {
// 	KKSG::ErrorCode nErrCode = KKSG::ERR_SUCCESS;
// 	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
// 	{
// 		nErrCode = KKSG::ERR_FAILED;
// 		LogError("Delete rolereport failed[%llu], errMsg: %s", m_roleId, GetErrStream().str().c_str());
// 	}
// 
// 	CUserData oUser((UINT32)nErrCode, NULL);
// 	CRpc::ReplyDelayRpc(m_rpcId, oUser);
// }

bool DelReportTask2::db_thread_process()
{
	char* pcBuffer = pdbi_->GetEscapStringBuff();
	SetMysqlError(MYSQL_ERR_FAILED);
	std::string sql = "delete from rolereport where _id=" + ToString(m_roleId);
	if (!pdbi_->Query(sql.c_str(), sql.size(), NULL))
	{
		GetErrStream() << "ErrDesc: " << pdbi_->GetError() << ", ErrNo: " << pdbi_->GetErrno();
		return false;
	}

	SetMysqlError(MYSQL_ERR_SUCESS);
	return false;
}

thread::TPTask::TPTaskState DelReportTask2::presentMainThread()
{
	KKSG::ErrorCode nErrCode = KKSG::ERR_SUCCESS;
	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		nErrCode = KKSG::ERR_FAILED;
		LogError("Delete rolereport failed[%llu], errMsg: %s", m_roleId, GetErrStream().str().c_str());
	}

	CUserData oUser((UINT32)nErrCode, NULL);
	CRpc::ReplyDelayRpc(m_rpcId, oUser);
	return DBTaskBase::presentMainThread()/*thread::TPTask::TPTASK_STATE_COMPLETED*/;
}